import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:线程池的实现
 * User: 邓大帅
 * Date: 2024-01-25
 * Time: 21:37
 */
class MyThreadPool {
    private BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque();
    public void submit(Runnable runnable) throws InterruptedException {
        blockingDeque.put(runnable);
    }
    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
               while (true) {
                   try {
                       Runnable runnable = blockingDeque.take();
                       runnable.run();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
            });
            thread.start();
        }
    }
}
public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int tmp = i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + tmp);
                }
            });
        }
    }
}
